package com.deanlib.cabinet.ui.recyclerview;

import android.content.Context;
import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @auther Dean
 * @create 2022/3/30
 */
public class VerticalItemDecoration extends RecyclerView.ItemDecoration {

    private int space = 0;//定义2个Item之间的距离

    public VerticalItemDecoration(Context context, int space){
        this.space = dip2px(context, space);
    }


    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view,
                               @NonNull RecyclerView parent,
                               @NonNull RecyclerView.State state) {
        if (parent.getAdapter()!=null) {
            int position = parent.getChildAdapterPosition(view);
//            int totalCount = parent.getAdapter().getItemCount();
            if (position >= 1) { //中间其它的
                outRect.top = space;
            }
        }
    }

    private int dip2px(Context context, float dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
